#include "Alarm.h"
#include "../TinyXML/tinyxml.h"
#include "../ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

Alarm::Alarm()
{

}

void Alarm::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);

    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    QMap<int, QList<QList<AlarmStruct>>> AlarmStructmap;
    Analysis(filePath, AlarmStructmap);
    QMap<int, QList<QList<AlarmStruct>>>::const_iterator it = AlarmStructmap.constBegin();
    while (it != AlarmStructmap.constEnd()) {
        for (int i = 0;i<  it.value().size();i++)
        {
            QList<AlarmStruct> _AlarmStructs =it.value().at(i);
            TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
            layout->addWidget(_TableWidgetForm);
            _TableWidgetForm->setGroupBoxTitle(fileName.left(fileName.size()-4));

            AlarmStruct _AlarmStructtemp;
            _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AlarmStructtemp.toFieldNames().split(',')));
            _TableWidgetForm->setTableWidgetHeaderType(_AlarmStructtemp.toFieldTypes());

            _TableWidgetForm->setEnableAddAndDel(true);
            _TableWidgetForm->setTablecolumnhide(1);// 隐藏

            for (int j = 0; j < _AlarmStructs.size(); j++)
            {
                int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
                _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(QString("%1").arg(_AlarmStructs.at(j).code)));
                _TableWidgetForm->getTableWidget()->item(rowCount, 0)->setData(Qt::UserRole, _AlarmStructs.at(j).id);
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(_AlarmStructs.at(j).name));
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(_AlarmStructs.at(j).desc));
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(_AlarmStructs.at(j).prompt));
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 4, new QTableWidgetItem(QString("%1").arg(_AlarmStructs.at(j).opt_code)));
                _TableWidgetForm->getTableWidget()->setItem(rowCount, 5, new QTableWidgetItem(_AlarmStructs.at(j).req_user_lv));
            }

            _TableWidgetForm->setTablecolumndisEdit(2);// 不可编辑
            _TableWidgetForm->setTablecolumndisEdit(3);// 不可编辑
            QList<TableWidgetForm*> item = m_pTableWidgetFormMap.value(it.key());
            m_pTableWidgetFormMap.insert(it.key(), item << _TableWidgetForm);
        }
        ++it;
    }
}

bool Alarm::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.size() > 0 && m_pTableWidgetFormMap.value(0).size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            QList<AlarmStruct> _AlarmStructs;
            for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                AlarmStruct _AlarmStruct;
                _AlarmStruct.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _AlarmStruct.code = _TableWidgetForm->getTableWidget()->item(k,0)->text().toInt();
                _AlarmStruct.name = _TableWidgetForm->getTableWidget()->item(k,1)->text();
                _AlarmStruct.desc = _TableWidgetForm->getTableWidget()->item(k,2)->text();
                _AlarmStruct.prompt = _TableWidgetForm->getTableWidget()->item(k,3)->text();
                _AlarmStruct.opt_code = _TableWidgetForm->getTableWidget()->item(k,4)->text().toInt();
                _AlarmStruct.req_user_lv = _TableWidgetForm->getTableWidget()->item(k,5)->text();
                _AlarmStructs.append(_AlarmStruct);
            }
            SaveXml(filePath, _AlarmStructs);
            return true;
        }
    }
    return false;
}

void Alarm::Analysis(const QString &path, QMap<int, QList<QList<AlarmStruct> > > &AlarmStructMap)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    //查找xml文件中第一个名为Alarms的节点
    TiXmlElement* rootElement = xmlDocument->FirstChildElement("Alarms");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        QList<AlarmStruct> AlarmStructs;
        //"Alarm"节点
        for(TiXmlElement* childElement = rootElement->FirstChildElement("Alarm");
            childElement != nullptr; childElement = childElement->NextSiblingElement("Alarm"))
        {
            AlarmStruct _AlarmStruct;
            childElement->QueryIntAttribute("id", &_AlarmStruct.id);
            childElement->QueryIntAttribute("code", &_AlarmStruct.code);
            _AlarmStruct.name = childElement->Attribute("name");
            _AlarmStruct.desc = childElement->Attribute("desc");
            _AlarmStruct.prompt = childElement->Attribute("prompt");
            childElement->QueryIntAttribute("opt_code", &_AlarmStruct.opt_code);
            _AlarmStruct.req_user_lv = childElement->Attribute("req_user_lv");
            AlarmStructs.append(_AlarmStruct);
        }
        AlarmStructMap.insert(0, QList<QList<AlarmStruct>>() << AlarmStructs);
    }

    delete xmlDocument;
}

bool Alarm::SaveXml(const QString &path, const QList<AlarmStruct> &AlarmStructMap)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    //查找xml文件中第一个名为Person的节点
    TiXmlElement* rootElement = xmlDocument->FirstChildElement("Alarms");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        for(TiXmlElement* childElement = rootElement->FirstChildElement("Alarm");
            childElement != nullptr; childElement = childElement->NextSiblingElement("Alarm"))
        {
            rootElement->RemoveChild(childElement);
        }
        for (auto _AlarmStruct : AlarmStructMap)
        {
            //元素类
            TiXmlElement* ChildxmlElement = new TiXmlElement("Alarm");
            ChildxmlElement->SetAttribute("id", _AlarmStruct.id);
            ChildxmlElement->SetAttribute("code", _AlarmStruct.code);
            ChildxmlElement->SetAttribute("name", _AlarmStruct.name.toStdString().c_str());
            ChildxmlElement->SetAttribute("desc", _AlarmStruct.desc.toStdString().c_str());
            ChildxmlElement->SetAttribute("prompt", _AlarmStruct.prompt.toStdString().c_str());
            ChildxmlElement->SetAttribute("opt_code", _AlarmStruct.opt_code);
            ChildxmlElement->SetAttribute("req_user_lv", _AlarmStruct.req_user_lv.toStdString().c_str());
            rootElement->LinkEndChild(ChildxmlElement);
        }
    }

    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
